package com.study.web.controller.goal;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.study.common.core.controller.AppBaseController;
import com.study.common.core.domain.R;
import com.study.common.core.page.TableDataInfo;
import com.study.goal.domain.PlanOccurrence;
import com.study.goal.domain.bo.PlanOccurrenceBo;
import com.study.goal.service.IPlanOccurrenceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 计划打卡信息
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController("/plan-occurrences")
public class PlanOccurrenceController extends AppBaseController {

    private final IPlanOccurrenceService planOccurrenceService;

    /**
     * 创建计划执行记录
     * @param bo
     * @return
     */
    @SaCheckLogin
    @PostMapping("/create")
    public R<Void> create(@Validated @RequestBody PlanOccurrenceBo bo){
        return toAjax(planOccurrenceService.create(bo));
    }

    /**
     * 启动计划执行
     * @param id
     * @return
     */
    @SaCheckLogin
    @PostMapping("/start")
    public R<Void> start(Long id){
        return toAjax(planOccurrenceService.start(id));
    }

    /**
     * 提交计划执行
     * @param id
     * @return
     */
    @SaCheckLogin
    @PostMapping("/submit")
    public R<Void> submit(Long id){
        return toAjax(planOccurrenceService.submit(id));
    }

    /**
     * 审批计划执行
     * @param id
     * @return
     */
    @SaCheckLogin
    @PostMapping("/approve")
    public R<Void> approve(Long id){
        return toAjax(planOccurrenceService.approve(id));
    }

    /**
     * 拒绝计划执行
     * @param id
     * @return
     */
    @SaCheckLogin
    @PostMapping("/reject")
    public R<Void> reject(Long id){
        return toAjax(planOccurrenceService.reject(id));
    }

    /**
     * Miss计划执行
     * @param id
     * @return
     */
    @SaCheckLogin
    @PostMapping("/miss")
    public R<Void> miss(Long id){
        return toAjax(planOccurrenceService.markMissed(id));
    }

    /**
     * 获取计划执行列表
     * @param assignmentId
     * @return
     */
    @SaCheckLogin
    @PostMapping("/list")
    public TableDataInfo<PlanOccurrence> list(Long assignmentId){
        return planOccurrenceService.listByAssignment(assignmentId);
    }


    /**
     * 获取计划执行列表
     * @param assignmentId 学生ID
     * @param date  时间
     * @return 列表
     */
    @SaCheckLogin
    @PostMapping("/listByDate")
    public R<List<PlanOccurrence>> listByDate(Long assignmentId, String date){
        LocalDateTime dateTime = (date != null && !"null".equals(date) && !date.trim().isEmpty())
                ? LocalDateTime.parse(date)
                : LocalDateTime.now();
        return R.ok(planOccurrenceService.listByAssignmentAndDate(assignmentId, dateTime));
    }

    /**
     * 获取学生当天计划执行列表
     * @param date  时间
     * @return 列表
     */
    @SaCheckLogin
    @PostMapping("/studentListByDate")
    public R<List<PlanOccurrence>> studentListByDate(String date){
        LocalDateTime dateTime = (date != null && !"null".equals(date) && !date.trim().isEmpty())
                ? LocalDateTime.parse(date)
                : LocalDateTime.now();
        return R.ok(planOccurrenceService.studentListByDate(getUserId(), dateTime));
    }
}
